<?php
/**
 * SVN Log Messages测试
 * @author Alan.H
 */
require_once "lib/SvnLog.php";
require_once "diff/Diff.php";
require_once 'diff/Diff/Renderer/Html/SideBySide.php';

$config = require_once 'config.php';
$log    = new SvnLog($config['url'], $config['username'], $config['password']);
$log->setRuntime(dirname(__FILE__) . '/runtime');
$log->setRevision([$config['start'] ? $config['start'] : 0, $config['end'] ? $config['end'] : 100]);
$results = $log->run();
krsort($results, SORT_NUMERIC);

/**
 * GET参数初始
 */
$revision = isset($_GET['revision']) ? $_GET['revision'] : null;
$path = isset($_GET['path']) ? $_GET['path'] : null;

// 文件比较
if (isset($path)) {
    $data = $results[$revision]['paths'][$path];
    $a = explode("\n", file_get_contents($data['old']));
    $b = explode("\n", file_get_contents($data['new']));

    $diff = new Diff($a, $b);
    $renderer = new Diff_Renderer_Html_SideBySide();
    echo "<div style='overflow-x: auto;overflow-y: auto; height: 450px;'>";
    echo $diff->Render($renderer);
    echo "</div>";
    die;
}

/**
 * HTML输出
 */
echo '<!DOCTYPE html>';
echo '<html lang="zh-CN">';
echo '<head>';
echo '<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">';
echo '<link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">';
echo '<link href="data:text/css;charset=utf-8," data-href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet" id="bs-theme-stylesheet">';
echo '<link href="http://aui.github.io/artDialog/css/ui-dialog.css" rel="stylesheet">';
echo '<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>';
echo '<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>';
echo '<script src="http://aui.github.io/artDialog/dist/dialog-plus.js"></script>';
echo '<script src="tableHeadFixer.js"></script>';
echo '<link rel="stylesheet" href="styles.css" type="text/css" charset="utf-8"/>';
echo '</head>';

echo '<body>';
echo '<div class="container">';
echo '<h1>SVN Log Messages</h1>';

// 版本表
$table = array();
$table[] = '<div id="revisionParent" style="height:350px;">';
$table[] = '<table class="table table-bordered table-hover" id="revision">';
$table[] = '<thead><tr><th>Revision</th><th>Actions</th><th>Author</th><th>Date</th><th>Msg</th></tr></thead>';
$table[] = '<tbody>';
foreach ($results as $result) {
    $class   = ($revision == $result['revision']) ? 'active' : '';
    $actions = join(',', $result['actions']);
    $message = str_replace("\n", " ", $result['msg']);
    $table[] = "<tr class='{$class}' style='cursor: pointer;' data-revision='{$result['revision']}'>";
    $table[] = "<td>{$result['revision']}</td>";
    $table[] = "<td>{$actions}</td>";
    $table[] = "<td>{$result['author']}</td>";
    $table[] = "<td>{$result['date']}</td>";
    $table[] = "<td>{$message}</td></tr>";
}
$table[] = '</tbody></table></div>';
echo join('',$table);

// 详情表
if ($revision) {
    echo "<h4>当前版本{$revision}详情</h4>";

    $paths = $results[$revision]['paths'];
    $table = array();
    $table[] = '<div id="pathsParent" style="height:350px;">';
    $table[] = '<table class="table table-bordered table-hover" id="paths">';
    $table[] = '<thead><tr><th>Path</th><th>Action</th></tr></thead>';
    $table[] = '<tbody>';
    foreach ($paths as $key => $path) {
        $table[] = "<tr style='cursor: pointer;' data-revision='{$revision}' data-path='{$key}' data-kind='{$path['kind']}'>";
        $table[] = "<td>{$path['path']}</td>";
        $table[] = "<td>{$path['action']}</td>";
    }
    $table[] = '</tbody></table></div>';
    echo join('',$table);
}

echo '</div>';
echo '<script>
    $(function() {
        $("#revision").tableHeadFixer();
        $("#paths").tableHeadFixer();

        $("#revision tbody tr").on("click", function() {
            if ($(this).hasClass("active")) {
                return ;
            }
            location.href = location.origin + location.pathname + "?revision=" + $(this).data("revision");
        });

        $("#paths tbody tr").on("click", function() {
            if ($(this).data("kind") == "dir") {
                return ;
            }

            var url = location.origin + location.pathname + "?revision=" + $(this).data("revision") + "&path=" + $(this).data("path");
            $.get(url, function(html) {
                return dialog({
                    title   : "版本差异比对",
                    content : html,
                    height  : 450,
                    padding : 10,
                    cancel  : function () {},
                    cancelValue: "关闭"
                }).showModal();
            }, "html");
        });
    });
    </script>';
echo '</body>';
echo '</html>';